<?php
/**
*
* @filename 收益方式
*/
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Bank as Banks;
use think\Db;

class Bank extends Common
{
    private $cModel;   //当前控制器关联模型
    
    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new Banks;   //别名：避免与控制名冲突
    }
    
    public function index()
    {
        $where = [];
		if(input('get.type')){
			$where['type'] = ['eq',input('get.type')];
		}
        if (input('get.search')){
            $where['name'] = ['like', '%'.input('get.search').'%'];
        }
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id desc';
        }
        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());
        $this->assign('dataList', $dataList);
		$this->assign('bankList',$this->getBankType());
        return $this->fetch();
    }
    public function create(){
        if (request()->isPost()){
            Db::startTrans();
            try{
                $data = input('post.');
                $result = $this->cModel->validate(CONTROLLER_NAME.'.add')->allowField(true)->save($data);
                $uid = $this->cModel->getLastInsID();
                // 提交事务
                if ($result && $uid){
                    Db::commit();
                    return ajaxReturn(lang('action_success'), url('index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ajaxReturn($e->getMessage());
            }
        }else{
			$this->assign('bankList',$this->getBankType());
            return $this->fetch();
        }
    }

    public function edit($id){
        if (request()->isPost()){
            $data = input('post.');
            //修改信息
            if (count($data) == 2){
                foreach ($data as $k =>$v){
                    $fv = $k!='id' ? $k : '';
                }
                $result = $this->cModel->validate(CONTROLLER_NAME.'.'.$fv)->allowField(true)->save($data, $data['id']);
            }else{
                $result = $this->cModel->validate(CONTROLLER_NAME.'.edit')->allowField(true)->save($data, $data['id']);
            }
           
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            if ($id > 0){
                $data = $this->cModel->get($id);
                
                $this->assign('data', $data);
				$this->assign('bankList',$this->getBankType());
                return $this->fetch();
            }
        }
    
    }

    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                Db::startTrans();
                try{
                    $id_arr = explode(',', $id);   //用户数据
					$rechModel = new \app\admin\model\Recharge;
					$cashModel = new \app\admin\model\Getcash;
                    $where1 = [ 'user_id' => ['in', $id_arr] ];
                    $where2 = [ 'id' => ['in', $id_arr] ];
                    $result = $this->cModel->where($where2)->delete();   //删除主表数据
                    $result2 = $rechModel->where($where1)->delete();   //删除充值记录
                    $result3 = $rechModel->where($where1)->delete();   //删除提现记录
                    // 提交事务
                    if ($result){
                        Db::commit();
                        return ajaxReturn(lang('action_success'), url('index'));
                    }else{
                        return ajaxReturn($this->cModel->getError());
                    }
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return ajaxReturn($e->getMessage());
                }
            }
        }
    }
	
	private function getBankType(){
		return [
			'1'	=> '浮动类',
			'2'	=> '固定类',
			'3'	=> '混合类'
		];
	}
}